﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

using com.crawler.common;
using com.crawler.components;

namespace com.crawler.winfrom
{
    public partial class FrmSiteManage : Form
    {
        /// <summary>
        /// 站点列表
        /// </summary>
        public List<BookSiteInfo> BookSiteList { get; set; }

        /// <summary>
        /// 修改的站点id
        /// </summary>
        public int SiteId { get; set; }

     

        public FrmSiteManage()
        {
            InitializeComponent();

            
        }

        /// <summary>
        /// 保存站点配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_save_Click(object sender, EventArgs e)
        {

            BookSiteInfo t_book_site = new BookSiteInfo();

            int t_site_id = 0;
            if (!int.TryParse(txt_id.Text.Trim(), out t_site_id))
                t_site_id = 0;

            if (SiteId != 0)
            {
                List<BookSiteInfo> t_list = BookSiteList.FindAll(delegate(BookSiteInfo t_site)
                {
                    return t_site.SiteId == SiteId;
                });

                if (t_list != null && t_list.Count > 0)
                {
                    t_book_site = t_list[0];
                }
            }

            t_book_site.SiteId = t_site_id;
            t_book_site.SiteName = txt_site_name.Text.Trim();
            t_book_site.SiteEncoding = txt_site_encoding.Text.Trim();
            t_book_site.IsVip = chk_vip.Checked;
            t_book_site.BookListUrl = txt_book_list_url.Text.Trim();
            t_book_site.BookListReg = txt_book_list_reg.Text.Trim();
            t_book_site.BookNameRegIndex = Convert.ToInt32(txt_book_name_reg_index.Text.Trim());
            t_book_site.BookUrlRegIndex = Convert.ToInt32(txt_book_url_reg_index.Text.Trim());
            t_book_site.BookReg = txt_book_reg.Text.Trim();
            t_book_site.BookNameRegIndex2 = Convert.ToInt32(txt_book_name_reg_index2.Text.Trim());
            t_book_site.AuthorNameRegIndex = Convert.ToInt32(txt_author_name_reg_index.Text.Trim());
            t_book_site.CategoryNameRegIndex = Convert.ToInt32(txt_category_name_reg_index.Text.Trim());
            t_book_site.SubCategoryNameRegIndex = Convert.ToInt32(txt_subcategory_name_reg_index.Text.Trim());
            t_book_site.ICONRegIndex = Convert.ToInt32(txt_icon_reg_index.Text.Trim());
            t_book_site.DescriptionRegIndex = Convert.ToInt32(txt_description_reg_index.Text.Trim());
            t_book_site.ChapterListUrlIndex = Convert.ToInt32(txt_chapter_list_index.Text.Trim());
            t_book_site.ChapterListReg = txt_chapter_list_reg.Text.Trim();
            t_book_site.ChapterNameRegIndex = Convert.ToInt32(txt_chapter_name_reg_index.Text.Trim());
            t_book_site.UpdateTimeRegIndex = Convert.ToInt32(txt_chapter_updatetime.Text.Trim());
            t_book_site.ChapterUrlRegIndex = Convert.ToInt32(txt_chapter_url_reg_index.Text.Trim());
            t_book_site.Memo = txt_memo.Text.Trim();
            t_book_site.ChapterContentReg = txt_chapter_content_reg.Text.Trim();
            t_book_site.ChapterContentFilterReg = txt_chapter_content_filter.Text.Trim();
            t_book_site.StartPageIndex = Convert.ToInt32(txt_start_page_index.Text.Trim());
            if (cmb_chapter_order.SelectedIndex == 0)
            {
                t_book_site.ChapterOrder = ChapterOrderType.ASC;
            }
            else
            {
                t_book_site.ChapterOrder = ChapterOrderType.DESC;
            }
            int t_end_page_index = 50;
            t_book_site.IsForbid = chk_is_used.Checked;
            if (!int.TryParse(txt_end_page_index.Text.Trim(), out t_end_page_index))
                t_end_page_index = 50;
            t_book_site.EndPageIndex = t_end_page_index;

            if (BookSiteList == null)
                BookSiteList = new List<BookSiteInfo>();

            if (SiteId == 0)
            {
                BookSiteList.Add(t_book_site);
            }

            try
            {
                bool t_ret = SerializationHelper.Save(BookSiteList, Consts.SiteFileName);
                if (t_ret)
                {
                    DBManager t_db_mgr = new DBManager();
                    Uri t_url = new Uri(txt_book_list_url.Text.Trim());

                    Match t_mc = Regex.Match(t_url.Host.ToString(), "\\S+\\.(\\S+)\\.", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    string t_table_name=string.Empty;
                    if (t_mc != null && t_mc.Groups.Count > 0)
                    {
                        t_table_name = "tb" + t_mc.Groups[1].Value.ToString();
                    }
                    //新建站点插入数据库
                    if (chkInsertDB.Checked)
                    {
                        int t_result = t_db_mgr.InsertBookSite(t_book_site.SiteId, t_book_site.SiteName, t_table_name, t_book_site.SiteEncoding,t_book_site.ChapterContentReg, t_book_site.ChapterContentFilterReg);

                        if (t_result <= 0)
                        {
                            MessageBox.Show("插入站点信息到数据库失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }

                    if (chk_create_table.Checked)
                    {
                        //成功了要创建存放章节的表
                       if(!string.IsNullOrEmpty(t_table_name))
                       {

                        
                            int t_create_ret = t_db_mgr.CreateChapterTable(t_table_name);

                            if (t_create_ret != -10)
                            {
                                MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                                this.DialogResult = DialogResult.OK;
                                this.Close();
                            }
                            else
                            {
                                MessageBox.Show("创建表结构失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                this.Close();
                            }
                        }
                        else
                        {

                            MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                    }
                    else
                    {

                        MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("保存站点设置失败" + ex.ToString());
                MessageBox.Show("保存出现异常", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 窗体加载站点属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmSiteManage_Load(object sender, EventArgs e)
        {
            if (SiteId != 0 && BookSiteList != null)
            {

                foreach (BookSiteInfo t_site_info in BookSiteList)
                {
                    if (t_site_info.SiteId == SiteId)
                    {
                        txt_id.Text = SiteId.ToString();
                        txt_site_name.Text = t_site_info.SiteName;
                        txt_site_encoding.Text=t_site_info.SiteEncoding;
                        chk_vip.Checked = t_site_info.IsVip;
                        txt_book_list_url.Text = t_site_info.BookListUrl;
                        txt_book_list_reg.Text = t_site_info.BookListReg;
                        txt_book_name_reg_index.Text=  t_site_info.BookNameRegIndex.ToString();
                        txt_book_url_reg_index.Text= t_site_info.BookUrlRegIndex.ToString();
                        txt_book_reg.Text=  t_site_info.BookReg ;
                        txt_book_name_reg_index2.Text=t_site_info.BookNameRegIndex2.ToString() ;
                        txt_author_name_reg_index.Text = t_site_info.AuthorNameRegIndex.ToString();
                        txt_category_name_reg_index.Text = t_site_info.CategoryNameRegIndex.ToString();
                        txt_subcategory_name_reg_index.Text=  t_site_info.SubCategoryNameRegIndex.ToString();
                        txt_icon_reg_index.Text=t_site_info.ICONRegIndex.ToString();
                        txt_description_reg_index.Text=t_site_info.DescriptionRegIndex.ToString();
                        txt_chapter_list_reg.Text=t_site_info.ChapterListReg.ToString();
                        txt_chapter_name_reg_index.Text=t_site_info.ChapterNameRegIndex.ToString();
                        txt_chapter_url_reg_index.Text=t_site_info.ChapterUrlRegIndex.ToString();
                        txt_chapter_updatetime.Text = t_site_info.UpdateTimeRegIndex.ToString();
                        txt_end_page_index.Text = t_site_info.EndPageIndex.ToString();
                        txt_chapter_list_index.Text = t_site_info.ChapterListUrlIndex.ToString();
                        txt_chapter_url_reg_index.Text = t_site_info.ChapterUrlRegIndex.ToString();
                        txt_start_page_index.Text = t_site_info.StartPageIndex.ToString();
                        chk_create_table.Checked = false;
                        txt_id.Enabled = false;
                        chkInsertDB.Checked = false;
                        chk_is_used.Checked = t_site_info.IsForbid;
                        if (t_site_info.ChapterOrder == ChapterOrderType.ASC)
                        {
                            cmb_chapter_order.SelectedIndex = 0;
                        }
                        else
                        {
                            cmb_chapter_order.SelectedIndex = 1;
                        }
                        txt_memo.Text = t_site_info.Memo;
                        txt_chapter_content_reg.Text = t_site_info.ChapterContentReg;
                        txt_chapter_content_filter.Text = t_site_info.ChapterContentFilterReg;
                    }
                }
            }
        }

       
    }
}
